<?php

namespace app\admin\controller\health\order;

use app\common\controller\Backend;
use app\health\model\HealthFuluOrder;
use think\Exception;

class FuluOrder extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new HealthFuluOrder;
    }

    /**
     * 订单列表
     * @return mixed|string|\think\response\Json
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $paginate           = $this->model
                ->where([])
                ->with(['orderItem', 'user'])
                ->order('id', 'desc')
                ->paginate($this->request->param('limit/d', 10))
                ->each(function ($item) {
                    $item->append(['order_status_txt']);
                });
            $data['rows']       = $paginate->items();
            $data['total']      = $paginate->total();
            $data['total_page'] = $paginate->lastPage();
            $this->success('', null, $data);
        }
        $this->assign('title', '福禄商品订单');
        return $this->fetch();
    }

    /**
     * 订单详情
     * @param $id
     * @return string
     * @throws Exception
     * @throws \think\exception\DbException
     */
    public function detail($id): string
    {
        $row = $this->model->get($id, ['orderItem', 'user']);
        if (!$row) {
            $this->error('未找到订单');
        }
        $row->append(['order_status_txt']);
        if ($this->request->isAjax()) {
            $this->success('订单详情', null, $row);
        }
        $this->assignconfig('row', $row);
        return $this->view->fetch();
    }
}